<template>
	 <div class='top-bar' id="top-bar">
		<div><span class="topbar-number">2</span></div>
		<div class='top-bar-right'>
			<div>
				<a v-link="link" v-if='loginStatus' class='top-bar-login' @click='showlogOut'>登出</a>
				<a v-link="link" @click="showLogin" class='top-bar-login' v-else>{{title}}</a>
			</div>
			<div class="icon-message vux-reddot top-bar-message"><a v-link="{name:'message'}"></a></div>
			<a href="http://fir.im/wnm7"><button class="top-bar-download">下载App</button></a>
			<div><confirm :show.sync="show" title="确定要退出登录吗?" cancel-text="取消" confirm-text="确定" @on-confirm="logout">
			</confirm></div>
		</div>
    </div>
</template>
<script>
import Confirm from 'vux-components/confirm'
import * as Account from "../../services/account.js"
import Toast from './Toast/index.js'
import Login from "./Login/index.js"
import {BusinessException} from "../../request"

export default {
	components: {
		Confirm
	},
	name:'TopBar',
	props:{
		link:Object,
		title:String,
	},
	data: function(){
		return {
			loginStatus: false,
			show: false
		}
	},
	methods:{
		showLogin(){
			Login.show()
		},
		showlogOut(){
			this.show = true
		},
		logout(){
			Account.LoginOut().then(()=>{
				Toast.show('退出登录成功','')
				this.loginStatus = false
			})
		}
	},
	async init(){
		await Account.CurrentUser().then((data)=>{
			this.loginStatus = true
		}).catch((e)=>{
			if (e instanceof BusinessException && e.info.errorSendCode) {
				Toast.show(e.info.errorSendCode.Message, { duration: durations.LONG })
				return
			}
			throw e
		});
	}
}
</script>
